using System;
using System.Collections;
using System.Threading;
using System.Web;

namespace Sedna.Core.Controller.Common
{
	public class UrlRedirectAction : AbstractAction
	{
		private string url;

		public string Url
		{
			get { return url; }
			set { url = value; }
		}

		public UrlRedirectAction():base()
		{
		}

		public override IActionResult Execute(IExecutionContext executionContext)
		{
			Execute(executionContext, ActionParams);
			return CreateResult(successStatus);
		}

		protected void Execute(IExecutionContext executionContext, IList paramsList)
		{
		    try
		    {
			    HttpContext.Current.Response.Redirect(Url + BuildParamsString(executionContext, paramsList));
		    }
		    catch (ThreadAbortException e)
		    {
		        //Ignore
		    }
		}

		protected string BuildParamsString(IExecutionContext executionContext, IList paramsList)
		{
			string urlParams = null;
			foreach(IActionParam param in paramsList)
			{
				if(urlParams == null)
				{
					urlParams = "?";
				}
				else
				{
					urlParams += "&";
				}
				urlParams += param.Name + "=" + param.GetValue(executionContext);
			}

			return urlParams;
			
		}
	}
}
